package com.y.controller;

import com.fasterxml.jackson.databind.util.JSONPObject;
import com.y.pojo.House;
import com.y.pojo.People;
import com.y.service.IHouseService;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
public class HouseController {

    /*添加房源*/
    @Autowired
    private IHouseService houseService;
    @RequestMapping("/insertHouse")
    public ModelAndView insertHouse(House house, ModelAndView mav,MultipartFile icon,HttpServletRequest request){

        People p = (People)request.getSession().getAttribute("people");
        house.setPid(p.getPid());

            String originalFilename = icon.getOriginalFilename();
            String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));

            //创建目标文件对象
            String savePath=request.getRealPath("/")+"icons/"+house.getHname()+suffix;
            File file=new File(savePath);
            if(!file.getParentFile().exists()){
                file.getParentFile().mkdir();
            }

            try {
                icon.transferTo(file);
            } catch (IOException e) {
                e.printStackTrace();
            }

            //读取路径
            String readPath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/icons/"+house.getHname()+suffix;

            house.setPicture(readPath);



        try {
            int i = houseService.insertHouse(house);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new ModelAndView("redirect:/queryHouse");
    }

    /*房主查询房源信息*/
    @RequestMapping("/queryHouse")
    public ModelAndView queryHouse(House house,ModelAndView mav, HttpServletRequest request){
        try {
            People p = (People)request.getSession().getAttribute("people");
            int pid = p.getPid();
            System.out.println("pid:::::"+pid);
            List<House> list = houseService.queryHouse(pid);
            mav.addObject("list",list);

            for(House house1:list){
                System.out.println("house:::::"+house1);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        mav.setViewName("guanli");
        return mav;
    }

    /*删除房源*/
    @RequestMapping("/delHouse")
    public ModelAndView delHouse(ModelAndView mav,HttpServletRequest request){
        String hid_str = request.getParameter("hid");
        int hid = Integer.parseInt(hid_str);
        try {
            int i = houseService.delHouse(hid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ModelAndView("redirect:/queryHouse");

    }

    /*修改房源信息*/
    @RequestMapping("/updateHouse")
    public ModelAndView updateHouse(House house,ModelAndView mav,HttpServletRequest request,MultipartFile icon){

        System.out.println("qwe");
        String originalFilename = icon.getOriginalFilename();
        String suffix = originalFilename.substring(originalFilename.lastIndexOf("."));

        //创建目标文件对象
        String savePath=request.getRealPath("/")+"icons/"+house.getHname()+suffix;
        File file=new File(savePath);
        if(!file.getParentFile().exists()){
            file.getParentFile().mkdir();
        }

        try {
            icon.transferTo(file);
        } catch (IOException e) {
            e.printStackTrace();
        }

        //读取路径
        String readPath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+request.getContextPath()+"/icons/"+house.getHname()+suffix;

        house.setPicture(readPath);
        try {
            int i = houseService.updateHouse(house);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return new ModelAndView("redirect:/queryHouse");
    }

    /*修改房源中转*/
    @RequestMapping("/selHouse")
    public ModelAndView selHouse(ModelAndView mav,HttpServletRequest request){
        String hid_str = request.getParameter("hid");
        int hid = Integer.parseInt(hid_str);



        try {
            House house = houseService.selHouse(hid);

            mav.addObject("house",house);
        } catch (Exception e) {
            e.printStackTrace();
        }
        mav.setViewName("update");
        return mav;
    }
    /*详情*/
    @RequestMapping("/seHouse")
    public ModelAndView seHouse(House house,ModelAndView mav,HttpServletRequest request) throws ParseException {
        String hid_str = request.getParameter("hid");
        int hid = Integer.parseInt(hid_str);

        try {
            House house1 = houseService.selHouse(hid);

            mav.addObject("house",house1);
        } catch (Exception e) {
            e.printStackTrace();
        }
        mav.setViewName("details");
        return mav;
    }
    /*查询全部*/
    @RequestMapping("/queHouse")
    public ModelAndView queHouse(ModelAndView mav){
        try {
            List<House> list = houseService.queHouse();
            mav.addObject("list",list);

            for(House house:list){
                System.out.println("house:::::"+house);
            }

        } catch (Exception e) {
            e.printStackTrace();
        }
        mav.setViewName("shouye");
        return mav;
    }

    /*添加房源中转*/
    @RequestMapping("/addHouse")
    public ModelAndView addHouse(ModelAndView mav,House house,HttpServletRequest request){
        People p = (People)request.getSession().getAttribute("people");
        int state = p.getState();
        System.out.println("state:::::::"+state);
        if (state==0){
            mav.setViewName("Payment");
        }else {
            mav.setViewName("fabu");
        }


        return mav;
    }


    /*分页*/
    @RequestMapping("/show")
    public ModelAndView show(ModelAndView mav,HttpServletRequest request){
        try {

            int count = houseService.getCount();
            int pages=count%3==0?count/3:count/3+1;

            String page_str = request.getParameter("page");
            int page = Integer.parseInt(page_str);


            List<House> allHouse = houseService.getAllHouse(page, pages);

            mav.addObject("list",allHouse);

            mav.addObject("page",page);
            mav.addObject("pages",pages);

            for(House house:allHouse){
                System.out.println("house::::"+house);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        mav.setViewName("shouye");
        return mav;
    }

    @RequestMapping("/shou")
    @ResponseBody
    public String shou(String str1, House house, HttpServletRequest request){
        String str11 = request.getParameter("str1");
        List<House> list=null;
        JSONArray ja=null;
        try {
             list = houseService.quHouse(str1);

             ja=new JSONArray();
            for(int i=0;i<list.size();i++){
                JSONObject jo=new JSONObject();
                jo.put("hid",list.get(i).getHid());
                jo.put("hname",list.get(i).getHname());
                jo.put("htype",list.get(i).getHtype());
                jo.put("location",list.get(i).getLocation());
                jo.put("picture",list.get(i).getPicture());
                jo.put("area",list.get(i).getArea());
                jo.put("tel",list.get(i).getTel());
                jo.put("price",list.get(i).getPrice());
                ja.put(jo);
            }


        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(ja.toString());
        return ja.toString();

    }

    /*删除全部*/
    @RequestMapping("/qbsc")
    public ModelAndView qbsc(ModelAndView mav,HttpServletRequest request){
        String pid_str = request.getParameter("pid");
        int pid = Integer.parseInt(pid_str);
        System.out.println("pid::::::::::pid:::::::::::::::::::::"+pid);

        try {
            List<House> list = houseService.delete(pid);
        } catch (Exception e) {
            e.printStackTrace();
        }

        return new ModelAndView("redirect:/queryHouse");
    }

    @RequestMapping("/deleteHouse")
    @ResponseBody
    public House deleteHouse(House house,HttpServletRequest request){
        String pid_str = request.getParameter("pid");
        int pid = Integer.parseInt(pid_str);

        house.setPid(pid);

	System.out.println("修改");
        return house;
    }


}
